﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MensUnoShop.Models;
using SenerPro;

namespace MensUnoShop.Areas.Admin.Models.DAL
{
    public class DALMarket
    {
        private DataClassesMenDataContext _dc;
        private Logger _log = new Logger(Config.logpath);

        public void GetManagerMarketChoiceList(int id, out List<string[]> outlist, out List<string[]> inlist)
        {
            outlist = new List<string[]>();
            inlist = new List<string[]>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var inl = _dc.UMarket.Where(m => m.MKstatus && m.UTagMarket.Any(tm=>tm.TMtagid==id) && m.MKtype!=(int)Config.MarketType.Mangazine);
                    if (inl != null)
                    {
                        var temp = inl.OrderBy(m => m.MKsn).Select(m=>new{title = m.MKsn + "_" + m.MKtitle,guid=m.MKguid});
                        foreach (var t in temp)
                        {
                            string[] st = new string[] { t.title, t.guid.ToString() };
                            inlist.Add(st);
                        }
                    }
                    var outl = _dc.UMarket.Where(m => m.MKstatus && !m.UTagMarket.Any(tm => tm.TMtagid == id) && m.MKtype != (int)Config.MarketType.Mangazine);
                    if (outl != null)
                    {
                        var temp = outl.OrderBy(m => m.MKsn).Select(m => new { title = m.MKsn + "_" + m.MKtitle, guid = m.MKguid });
                        foreach (var t in temp)
                        {
                            string[] st = new string[] { t.title, t.guid.ToString() };
                            outlist.Add(st);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerMarketChoiceList", ex.ToString());
            }
        }

        public bool SetManagerMarketChoiceIn(int tid, string[] mklist)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    List<UTagMarket> utmlist = new List<UTagMarket>();
                    foreach (string mk in mklist)
                    {
                        var mt = _dc.UTagMarket.SingleOrDefault(tm => tm.TMtagid == tid && tm.TMmkguid == new Guid(mk));
                        if (mt == null)
                        {
                            UTagMarket utm = new UTagMarket();
                            utm.TMmkguid = new Guid(mk);
                            utm.TMsequence = 0;
                            utm.TMstatus = true;
                            utm.TMtagid = tid;
                            utmlist.Add(utm);
                        }
                    }
                    _dc.UTagMarket.InsertAllOnSubmit(utmlist);
                    _dc.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetManagerMarketChoiceIn", string.Join("|||",mklist)+"\r\n"+ex.ToString());
            }
            return false;
        }

        public bool SetManagerMarketChoiceOut(int tid, string[] mklist)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var mt = _dc.UTagMarket.Where(tm => tm.TMtagid == tid && mklist.Contains(tm.TMmkguid.ToString().ToLower()));
                    if (mt != null)
                    {
                        _dc.UTagMarket.DeleteAllOnSubmit(mt);
                    }

                    _dc.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetManagerMarketChoiceOut", ex.ToString());
            }
            return false;
        }
    }
}